package com.ita.osc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.osc.bean.Dish;
import com.ita.osc.service.OrderManager;
import com.ita.osc.serviceImpl.OrderManagerImpl;
import com.ita.osc.util.JsonUtil;

/**
 * Servlet implementation class ListOrderDishServlet
 */
public class ListOrderDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    OrderManager orderManager = new OrderManagerImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListOrderDishServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String oid = request.getParameter("oid");
		Map<Dish, Integer> cartList = orderManager.listDishByOrderId(oid);
		PrintWriter writer = response.getWriter();
		if(cartList != null){			
			StringBuffer stringBuffer = new StringBuffer();
			for (Dish dish : cartList.keySet()) {
				String str = JsonUtil.objetc2Json(dish);
				str = str.substring(0, str.length()-1) + ",\"quantity\":" + cartList.get(dish) + "},";
				stringBuffer.append(str);
			}
			String json = "";
			if(stringBuffer.toString().length() > 0){
				json = "[" + stringBuffer.toString().substring(0,stringBuffer.toString().length()-1) + "]";
			}
			writer.println(json);
		}else {
			writer.println("");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
